//
//  RepairOrderListViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/6/26.
//

import UIKit
import CollectionKit
import JXSegmentedView

class RepairOrderListViewController: UIViewController {

  private var collectionView = CollectionView()

  var type: RepairOrderType = .waitingRepair

  override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor(hex: 0xFAFAFA)
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }

    let dataSource = ArrayDataSource<RepairOrderModel>(data:[
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
    ])

    let viewSource = ClosureViewSource<RepairOrderModel, RepairOrderContentView> { model, index in
      return R.nib.repairOrderContentView(owner: self)!
    } viewUpdater: { item, model, index in

    }

    let sizeSource = { (index: Int, data: RepairOrderModel, size: CGSize) in
      return CGSize(width: size.width - 30, height: 150)
    }

    let inset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)

    let itemProvider = BasicProvider<RepairOrderModel, RepairOrderContentView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 8, justifyContent: .center)
        .inset(by: inset)
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jumpToDetail(index: context.index)
    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])
  }

  func jumpToDetail(index: Int) {

    let vc = RepairOrderDetailViewController()
    vc.type = type
    navigationController?.pushViewController(vc, animated: true)

  }


  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */
  
}

extension RepairOrderListViewController: JXSegmentedListContainerViewListDelegate {

  func listView() -> UIView {
    return self.view
  }
}
